package com.ly.hotel.spainterface.jg.view;

import android.app.Dialog;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.spainterface.jg.bean.MassageBean;
import com.ly.hotel.spainterface.jg.bean.OnSwipeCardListener;
import com.ly.hotel.spainterface.jg.bean.gson.handcard.AdministratorInfo;
import com.ly.hotel.spainterface.jg.bean.gson.handcard.AdministratorInfoResult;
import com.ly.hotel.spainterface.jg.bean.gson.handcard.ClockOther_EmployInfo;
import com.ly.hotel.spainterface.jg.utils.AvoidDoubleClickedUtils;
import com.ly.hotel.spainterface.jg.utils.DecToHexUtils;
import com.ly.hotel.spainterface.jg.utils.GsonUtils;
import com.ly.hotel.spainterface.jg.utils.OkHttpUtils;
import com.ly.hotel.spainterface.jg.utils.SwipeCardUtils;

/**
 * Created by ThinkPad on 2018/2/10.
 */

public class CustomSettingDialogFragment extends DialogFragment implements OnSwipeCardListener {

    private CustomLoadingDialog loadingDialog;
    private String cardIdHex;
    private String cashierIP;
    private String cashierPORT;
    private int[] judgeNum = {0,1,2};
    private LinearLayout settingLinearLayout;
    private SwipeCardUtils swipeCardUtils;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    swipeCardUtils.cancelBandNo(false);
                    String resultString = msg.obj.toString();
                    if(msg.arg1 == judgeNum[0]){
                        Log.d("JgActivity", "服务员ID号转手牌号接口返回：" + resultString);
                        loadingDialog.close();
                        AdministratorInfoResult administratorInfoResult = GsonUtils.parseSimpleJson(resultString,AdministratorInfoResult.class);
                        if(administratorInfoResult.isSuccess()){
                            AdministratorInfo administratorInfo = administratorInfoResult.getResult();
                            if(administratorInfo.getErrorMsg().equals("") && administratorInfo.getOperatorName().equals("系统管理员")){
                                settingLinearLayout.setVisibility(View.VISIBLE);
                            }else{
                                dismiss();
                                CustomToast customToast = CustomToast.makeText(getActivity(),"非服务员(系统管理员)手牌！",Toast.LENGTH_LONG,false);
                                customToast.setGravity(Gravity.CENTER,0,0);
                                customToast.showMyToast(5000);
                            }
                        }
                    }else if(msg.arg1 == judgeNum[1]){

                    }
                    break;
                case 2:
                    swipeCardUtils.cancelBandNo(false);
                    dismiss();
                    if(loadingDialog != null){
                        loadingDialog.close();
                    }
                    CustomToast customToast = CustomToast.makeText(getActivity(),"网络异常，请呼叫服务员处理！",Toast.LENGTH_LONG,false);
                    customToast.setGravity(Gravity.CENTER,0,0);
                    customToast.showMyToast(5000);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.5), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.windowAnimations = R.style.customDialogAnim;
        window.setAttributes(layoutParams);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.jg_setting_funciton_layout,container,false);
        ImageButton settingClose =  view.findViewById(R.id.setting_close);
        ImageButton goodsHide = view.findViewById(R.id.goods_hide);
        ImageButton goodsShow = view.findViewById(R.id.goods_show);
        ImageButton goodsSort = view.findViewById(R.id.goods_sort);
        ImageButton goodsPicScanning = view.findViewById(R.id.goods_pic_scanning);
        settingLinearLayout = view.findViewById(R.id.setting_lineralayout);

        Bundle bundle = getArguments();
        MassageBean massageBean = (MassageBean) bundle.getSerializable("massageBean");
        String serverIP = bundle.getString("serverIP");
        String serverPORT = bundle.getString("serverPORT");
        cashierIP = bundle.getString("cashierIP");
        cashierPORT = bundle.getString("cashierPORT");

        final Bundle bundle2 = new Bundle();
        bundle2.putSerializable("massageBean",massageBean);
        bundle2.putString("serverIP",serverIP);
        bundle2.putString("serverPORT",serverPORT);
        bundle2.putString("cashierIP", cashierIP);
        bundle2.putString("cashierPORT", cashierPORT);

        swipeCardUtils = new SwipeCardUtils();
        swipeCardUtils.setOnSwipeCardListener(this);
        swipeCardUtils.swipecard();

        // 关闭系统设置窗口
        settingClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                swipeCardUtils.cancelBandNo(false);
                dismiss();
            }
        });

        // 点击商品隐藏
        goodsHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AvoidDoubleClickedUtils.isNotDoubleClicked()){// 如果点击的间隔少于
                    return;
                }
                CustomGoodsHideDialog goodsHideDialog = new CustomGoodsHideDialog();
                goodsHideDialog.setCancelable(false);
                goodsHideDialog.setArguments(bundle2);
                goodsHideDialog.show(getFragmentManager(),"CustomGoodsHideDialog");
            }
        });

        // 点击商品显示
        goodsShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AvoidDoubleClickedUtils.isNotDoubleClicked()){// 如果点击的间隔少于500毫秒
                    return;
                }
                CustomGoodsShowDialog goodsShowDialog = new CustomGoodsShowDialog();
                goodsShowDialog.setCancelable(false);
                goodsShowDialog.setArguments(bundle2);
                goodsShowDialog.show(getFragmentManager(),"CustomGoodsShowDialog");
            }
        });

        // 点击商品排序
        goodsSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MyApplication.getContext(),"商品排序",Toast.LENGTH_SHORT).show();

            }
        });

        // 点击扫描商品图片
        goodsPicScanning.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AvoidDoubleClickedUtils.isNotDoubleClicked()){// 如果点击的间隔少于
                    return;
                }
                CustomGoodsScanningDialog goodsScanningDialog = new CustomGoodsScanningDialog();
                goodsScanningDialog.setCancelable(false);
                goodsScanningDialog.setArguments(bundle2);
                goodsScanningDialog.show(getFragmentManager(),"CustomGoodsScanningDialog");
                Toast.makeText(MyApplication.getContext(),"扫描商品图片",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

    @Override
    public void swipeCardListener(String cardId) {
        Log.d("JgActivity","getBandNo---" + cardId);
        cardIdHex = new DecToHexUtils().getDecToHex(cardId);
        Log.d("JgActivity","getBandNo---" + cardIdHex);
        loadingDialog = new CustomLoadingDialog(getActivity(),"手牌验证中...");
        loadingDialog.show();
        cardIdToCardNo();
    }

    // 调用手牌ID转手牌号接口
    public void cardIdToCardNo(){
        ClockOther_EmployInfo clockOther_employInfo = new ClockOther_EmployInfo();
        clockOther_employInfo.setEmployCardId(cardIdHex);
        Gson gson = new Gson();
        String jsonString = gson.toJson(clockOther_employInfo);
        String url = "http://" + cashierIP + ":" + cashierPORT + "/TsInterface/ClockOther_GetEmployInfo";
        OkHttpUtils.getInstance().postJsonResource(jsonString,url,handler,judgeNum[0]);
    }
}
